/*
 *   Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *   Licensed under the GNU General Public License v3 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *        http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *   under the License.
 */
package xperiment.metaphor.model.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import metaphor.service.ServiceException;
import org.apache.commons.lang.builder.CompareToBuilder;
import xperiment.metaphor.model.DomainObject;
import xperiment.metaphor.model.ModelException;
import xperiment.metaphor.model.Persistent;
import xperiment.metaphor.model.SaveException;
import xperiment.metaphor.model.XML;

/**
 * Relationship between two entities
 * @author Mark P Ashworth <javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
public class Relationship extends DomainObject implements Persistent, XML, Comparable {

    /* The unique database id for the object */
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "relationship_id", unique = true, nullable = false)
    private Serializable id;

    /* The subject of the relationship */
    private Entity subject;

    /** Enabled */
    @Column(name="enabled", unique=false, nullable=true)
    private boolean enabled = true;

    /**
     * Default constructor
     */
    public Relationship() {
        super();
    }

    /**
     * Constructs the relation to the subject entity
     * @param subject The subject of the relationship
     */
    public Relationship(Entity subject) {
        this.subject = subject;
    }

    /**
     * The unique database id for the object
     * @return Long
     */
    public Serializable getId() {
        return id;
    }

    /**
     * The subject of the relationship
     * @return Entity
     */
    public Entity getSubject() {
        return this.subject;
    }

    /**
     * Whether the object is enabled
     * @return boolean
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Saves the relationship
     * @return Relationship
     * @throws xperiment.metaphor.model.ModelException If there was an error
     */
    public Relationship save() throws ModelException {

        try {
            Relationship r = (Relationship) getDao().persist(this);
            this.id = r.getId();
            return this;
        } catch (ServiceException e) {
            throw new SaveException("Unable to save relationship", e);
        }
    }

    /**
     * Compares relationships based upon the subject entity and the class of the
     * relationship. Subclasses should override
     * to compare on their fields.
     * @param o The object to compare to
     * @return int
     */
    public int compareTo(Object o) {
        Relationship rhs = (Relationship) o;
        return new CompareToBuilder()
                .append(getSubject(), rhs.getSubject())
                .append(getClass(), rhs.getClass())
                .toComparison();
    }

    public String toXML() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
